﻿using HslCommunication.Core;
using PLC.Domain.Dto;
using PLC.Domain.Entity;
using PLC.Domain.Enum;
using PLC.Domain.Models;
using PLC.Domain.Service.IRepository;

namespace PLC.Domain.ScheduledTasks.Strategy.Read;
/// <summary>
/// 读取过程数据的码
/// </summary>
public class ReadProcessData:BaseRead
{
    private readonly IPLCRepository _plcRepository;

    public ReadProcessData(IPLCRepository plcRepository)
    {
        _plcRepository = plcRepository ?? throw new ArgumentNullException(nameof(plcRepository));
    }
    public override EnumReadGroup ReadGroup { get; protected set; } = EnumReadGroup.过程数据;
    public override void Read(IReadWriteNet readWriteNet, PLCConfig plcConfig, List<DtoDataConfig> readConfigList, PLCReadModel plcReadModel)
    {
        if (readConfigList.Count == 0)
        {
            return;
        }
        // 默认读取最后一条的数据
        // 读取的数据集合
        var dataList = new List<PLCDataModel>();
        var upperEndPlateCode = string.Empty;
        var lowerEndPlateCode = string.Empty;

        // 这边默认使用第一个的数据类型 如果有很多个  也只采用第一个的  逻辑上来说一台设备 只能收集一种
        foreach (var config in readConfigList)
        {
            var data = _plcRepository.ReadData(readWriteNet, config.Register, (EnumDataType)config.DataType,
                (EnumDataEncoding)(config.Encoding ?? 0), config.DataLength);
            if (config.CodeType==null)
            {
                continue;
            }else if (config.CodeType==(int)EnumProcessDataType.下端板码)
            {
                lowerEndPlateCode = data;
                continue;
            }else if (config.CodeType == (int)EnumProcessDataType.上端板码)
            {
                upperEndPlateCode = data;
                continue;
            }
            var description = GetValueDescription(data, config);
            var precessData = new PLCDataModel()
            {
                DataType = config.DataType,
                Description = description,
                Register = config.Register,
                Value = data
            };
           dataList.Add(precessData);
        }
        //组装数据
        if (readConfigList.Any(p=>p.CodeType==(int)EnumProcessDataType.气密过程数据))
        {
            plcReadModel.AirtightData = new AirtightData()
                .SetLowerEndPlateCode(lowerEndPlateCode)
                .SetUpperEndPlateCode(upperEndPlateCode)
                .SetData(dataList).Build();
        }else if (readConfigList.Any(p => p.CodeType == (int)EnumProcessDataType.维修过程数据))
        {
            plcReadModel.RepairAirtightData = new RepairAirtightData()
                .SetLowerEndPlateCode(lowerEndPlateCode)
                .SetUpperEndPlateCode(upperEndPlateCode)
                .SetData(dataList).Build();
        }else if (readConfigList.Any(p=>p.CodeType==(int)EnumProcessDataType.装堆机压合过程))
        {
            plcReadModel.PressureData = new PressureData()
                .SetLowerEndPlateCode(lowerEndPlateCode)
                .SetUpperEndPlateCode(upperEndPlateCode)
                .SetData(dataList).Build();
        }

    }
}